package rs;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.nutz.json.Json;

import rs.util.Log;
import rs.util.StringUtil;

/**
 * 负责由工单产出产品,并将产品置入仓库.
 * @author wangpengcheng
 *
 */
public class JobProcessor implements Runnable{
	
	public JobDesc jobDesc;
	
	public JobProcessor(JobDesc jobDesc){
		this.jobDesc = jobDesc;
	}
	
	@Override
	public void run() {
		try {
			work(jobDesc);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void work(JobDesc jobDesc) throws MalformedURLException, IOException, InterruptedException{
		// 1. 将描述解析成job对象
		//JobDesc jobDesc = JobParser.work(jobJson);
		// 2. 生成crawler  (in processJobDesc)
		// -3. 组装proxy (在crawler内部实现)
		// 4. crawler根据job.url获取html内容 (in processJobDesc)
		Map<String, Object> model = new HashMap<String, Object>();
		processJobDesc(jobDesc, model);
		// 5. 生成extractor按照job描述抽取model属性, 置于伪model中 (in processJobDesc)
		// 6. 如job描述含有next,则生成url后重复4-6  (in processJobDesc)
		
		// 7. 调用model_fixer将伪model加工成model
		if(StringUtil.notEmpty(jobDesc.id)){
			model = ModelFixer.fix(model, jobDesc.id);
		}
		// 8. 将model置入待发送队列,结束
		Log.info("Before SEND: " + Json.toJson(model));
		ModelStore.store.put(model);
		
	}
	
	private void processJobDesc(JobDesc jobDesc, Map<String, Object> model) throws MalformedURLException, IOException{
		//获取url, 处理
		String url = StringUtil.format$URL(jobDesc.url, model);
		Log.log("ProcessJobDesc:" + url);
		// 获取url文档.
		Crawler crawler = new Crawler();
		Document doc;
		doc = crawler.work(url);
		// 解析文档.
		Extractor extractor = new Extractor();
		extractor.work(doc, jobDesc.props, model);
		//递归遍历,如有需在下一页面获取的属性.
		if(jobDesc.next != null && StringUtil.notEmpty(jobDesc.next.url)){
			Log.log("Next: " + jobDesc.next.url);
			processJobDesc(jobDesc.next, model);
		}
	}
	
	
	public static void main(String[] args) {
		
	}

	

}
